package mysql

import (
	"news/common"
	"news/constStatus"
	"news/models"

	"go.uber.org/zap"
)

// 判断管理员用户是否创建
func UserAdminExit(username string) (err error) {
	sqlStr := `select count(id) from user_admin where username = ?`
	var count int
	if err := db.Get(&count, sqlStr, username); err != nil {
		return constStatus.ErrorNOTTable // 连接表错误
	}
	if count > 0 {
		return constStatus.ErrorUserExist
	}
	return
}

// 管理员信息入库动作
func CreateAdminUserData(p *models.UserAdmin) (err error) {
	p.PasswordHash = common.EncryptPassword(p.PasswordHash)
	sqlstr := `insert into user_admin
			(username,password_hash,email,role,status,del_status) 
			values(?,?,?,?,?,?)`
	_, err = db.Exec(sqlstr, p.Username, p.PasswordHash, p.Email, p.Role, p.Status, p.DelStatus)
	if err != nil {
		zap.L().Error("CreateAdminUserData failed", zap.String("sqlStr:", sqlstr),
			zap.Error(err))
		return err
	}
	return err

}
